package com.qdxwx.manage.controller;

import com.qdxwx.common.config.ProjectConfig;
import com.qdxwx.data.service.account.UserAccountService;
import com.qdxwx.data.service.article.ArticleService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.menu.MenuService;
import com.qdxwx.data.service.order.OrderDetailService;
import com.qdxwx.data.service.order.OrderService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.data.service.user.UserService;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.account.UserAccount;
import com.qdxwx.models.article.ArticleQuery;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.OrderStatus;
import com.qdxwx.models.enums.OrderType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.manager.Menu;
import com.qdxwx.models.order.OrderDetailQuery;
import com.qdxwx.models.order.OrderQuery;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.user.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页 业务处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
public class MainController extends BaseController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserAccountService userAccountService;

    // 系统首页
    @GetMapping({"","/default.html"})
    public String showMain(ModelMap mmap) {
        // 取身份信息
        Manager manager = SessionUtils.getManager();
        // 根据用户id取出菜单
        List<Menu> menuList = menuService.getListByManager(manager, 0);

        mmap.put("menuList", menuList);
        mmap.put("manager", manager);
        //mmap.put("rootId", rootId);
        mmap.put("projectName", ProjectConfig.getName());
        mmap.put("version", ProjectConfig.getVersion());
        return "iframe";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        Manager manager = SessionUtils.getManager();
        String loginType;
        Teacher teacher = teacherService.getByMobile(manager.getMobile());
        if(teacher != null){
            loginType = "teacher";
            mmap.put("teacher", teacher);
        }else {
            loginType = "manager";
        }
        mmap.put("manager", manager);
        Map<String, Object> webCountMap = getWebCountMap(loginType);
        mmap.put("webCountMap", webCountMap);
        mmap.put("version", ProjectConfig.getVersion());
        switch (loginType) {
            case "manager":
                return "main";
            case "teacher":
                UserAccount userAccount = userAccountService.getByUserId(manager.getUserId());
                if (userAccount == null){
                    System.out.println("userAccount:null");
                    return error404;
                }
                mmap.put("userAccount", userAccount);
                return "main_teacher";
            default:
                return error404;
        }
    }

    public Map<String, Object> getWebCountMap(String loginType) {
        Map<String, Object> webCountMap = new HashMap<>();
        switch (loginType) {
            case "manager":
                //总销售额
                OrderQuery query = new OrderQuery();
                query.setStatus("SUCCESS");
                long payAmount = orderService.sumPayAmount(query);
                webCountMap.put("payAmount", payAmount);
                //今天销售额
                query.setCompanyId(null);
                query.setCreateTime(LocalDateTime.now());
                long sumToDayMoney = orderService.sumPayAmount(query);
                webCountMap.put("sumToDayMoney", sumToDayMoney);
                //今日成交
                int toDayDeal = orderService.count(query);
                webCountMap.put("toDayDeal", toDayDeal);
                //所有文章数量
                int articleCount = articleService.count(new ArticleQuery());
                webCountMap.put("articleCount", articleCount);//所有文章数量
                //所有用户数量
                long userCount = userService.count(new UserQuery());
                webCountMap.put("userCount", userCount);//所有用户数量
                //所有课程数量
                int courseCount = courseService.count(new CourseQuery());
                webCountMap.put("courseCount", courseCount);//所有课程数量
                //所有订单数
                int orderCount = orderService.count(new OrderQuery());
                webCountMap.put("orderCount", orderCount);//所有订单数
                //已支付订单数
                int orderSuccessCount = orderService.countByState("SUCCESS");
                webCountMap.put("orderSuccessCount", orderSuccessCount);//所有已支付订单数
                return webCountMap;
            case "teacher":
                //我的课程数量
                Manager manager = SessionUtils.getManager();
                CourseQuery courseQuery = new CourseQuery();
                courseQuery.setIsAvailable(1);
                courseQuery.setUserId(manager.getUserId());
                int myCourseCount = courseService.count(courseQuery);
                webCountMap.put("myCourseCount", myCourseCount);
                long myUserCount = userService.countMyStudent(manager.getUserId());
                webCountMap.put("myUserCount", myUserCount);
                //我成交订单数
                OrderDetailQuery orderDetail = new OrderDetailQuery();
                orderDetail.setOrderType(OrderType.COURSE.toString());
                orderDetail.setAuthStatus(OrderStatus.SUCCESS.toString());
                orderDetail.setUserId(manager.getUserId());
                long myOrderSuccessCount = orderDetailService.countByCourse(orderDetail);
                webCountMap.put("myOrderSuccessCount", myOrderSuccessCount);
                //我的订单数
                orderDetail.setAuthStatus(null);
                long myOrderCount = orderDetailService.countByCourse(orderDetail);
                webCountMap.put("myOrderCount", myOrderCount);
                return webCountMap;
            default:
                return null;
        }
    }
}
